<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2019/5/24
 * Time: 9:37
 */
namespace app\admin\controller;
use think\Controller;
use think\Request;
use app\admin\model\Keywords as Keyword;

class Keywords extends Controller
{
    /**加载表格
     * @return mixed
     */
    public function show_list(){
        return NewBuilder::make('table')
            ->addTableHeaderAllDel('keywords/del','全部删除')
            ->addTableHeaderAdd('keywords/show_add','添加')
            ->addTableHeaderSousuo('','搜索')
            ->addTableColumn('id','text','ID',null,null,100)
            ->addTableColumn('lanmu','text','页面')
            ->addTableColumn('keyword','text','关键词')
            ->addTableColumn('descriptions','text','关键词描述')
            ->addTableColumn('status','switch','状态',['开启','关闭'],'keywords/change')
            ->addTableRightHeader('操作')
//            ->addTableRight('detail','keywords/detail','查看')
            ->addTableRight('edit','keywords/show_edit','修改')
            ->addTableRight('del','keywords/del','删除')
            ->addTablePage('keywords/page')
            ->fetch();
    }

    /**加载数据
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function page(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $limit = $request->param('limit',10);
        $data = Keyword::where('id','>',0);
        if($tiaojian){
            $data = $data->where('id|keywordss|descriptions','like',"%$tiaojian%");
        }
        $data = $data->order('create_time desc')
            ->paginate($limit)
            ->toArray();
        foreach ($data['data'] as $key=>$value){
            if($value['lanmu']){
                $clomn = \app\admin\model\Column::where('id',$value['lanmu'])->findOrEmpty()->toArray();
                if($clomn){
                    $value['lanmu'] = $clomn['name'];
                }
            }else{
                $value['lanmu'] = '全部';
            }
            $data['data'][$key] = $value;
        }
        return json_page_response($data);
    }

    /**修改状态
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function change(Request $request){
        $id = $request->param('id');
        $data = Keyword::where('id',$id)
            ->findOrEmpty()
            ->toArray();
        if($data['status']==1){
            $data['status'] =0;
        }else{
            $data['status'] = 1;
        }
        if(Keyword::where('id',$data['id'])->update([
            'status'=>$data['status']
        ])){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }

    /**加载添加页面
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_add(Request $request){
        $column = \app\admin\model\Column::where('id','>',0)
            ->select()->toArray();
        $select = [0=>'全部'];
        foreach ($column as $value){
            $select[$value['id']] = $value['name'];
        }
        return NewBuilder::make('form')
            ->addAction('keywords/add')
            ->addInput('keyword','关键词')
            ->addTextarea('descriptions','关键词描述')
            ->addSelect('lanmu','选择页面',null,$select,true)
            ->addSwitch('status','状态',1)
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**添加
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function add(Request $request){
        $data = $request->param();
        $data['create_time'] = time();
        $data['update_time'] = time();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if(Keyword::create($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**加载添加
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function show_edit(Request $request){
        $id = $request->param('id');
        $data = Keyword::where('id',$id)->findOrEmpty()->toArray();
        $column = \app\admin\model\Column::where('id','>',0)
            ->select()->toArray();
        $select = [0=>'全部'];
        foreach ($column as $value){
            $select[$value['id']] = $value['name'];
        }
        return NewBuilder::make('form')
            ->addAction('keywords/edit')
            ->addNone('id',$id)
            ->addInput('keyword','关键词',$data['keyword'])
            ->addTextarea('descriptions','关键词描述',$data['descriptions'])
            ->addSelect('lanmu','选择页面',$data['lanmu'],$select,true)
            ->addSwitch('status','状态',$data['status'])
            ->addFormButton('submit','提交')
            ->addFormButton('back','返回')
            ->fetch();
    }

    /**修改
     * @param Request $request
     * @return \think\response\Redirect
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function edit(Request $request){
        $data = $request->param();
        $data['update_time'] = time();
        if(isset($data['status'])&&$data['status']=='on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        if(Keyword::where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }

    /**删除
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del(Request $request){
        $id = $request->param('id');
        if(Keyword::where('id','in',$id)->delete()){
            return json_my_response('修改成功',200);
        }else{
            return json_my_response('修改失败');
        }
    }
}